package com.jiajia.study.nio.chapter01;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

public class TestNio05 {
    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        byteBuffer.put(new byte[]{'a','b','c','d'});
        byteBuffer.flip();

        while (byteBuffer.hasRemaining()){
            System.out.println((char) byteBuffer.get());
        }
        // 切换到写模式
        byteBuffer.clear();

        byteBuffer.put("test write".getBytes(StandardCharsets.UTF_8));

        // 切换成读模式
        byteBuffer.flip();
        while (byteBuffer.hasRemaining()){
            System.out.println((char) byteBuffer.get());
        }
        //  limit = position,position -> 0 ,像手风琴一样可以来回读
        System.out.println("rewind()========================");
        byteBuffer.rewind();
        while (byteBuffer.hasRemaining()){
            System.out.println((char)byteBuffer.get());
        }
//        byteBuffer.clear();
    }
}
